﻿
$String=$class.create("$lang.String");
$String.prototype = {
		
	initialize : function(){},
	
	render : function(str){
		if(str&&str!="null"){
			return typeof(str)=="object"?"":str;
		}
		return "";
	},
	
	toArray : function(str){
		var s = str.replace(/;/g,",");
		return s.split(",");
	},
	
	trim : function(){
		var _re,_argument = arguments[0] || " ";
		typeof(_argument)=="string"?(_argument == " "?_re = /(^\s*)|(\s*$)/g : _re = new RegExp("(^"+_argument+"*)|("+_argument+"*$)","g")) : _re = _argument;
		return this.replace(_re,"");
	},
	
	stripTags : function(){
		return this.replace(/<\/?[^>]+>/gi, '');
	},
	
	cint : function(){
	    return this.replace(/\D/g,"")*1;
	},
	
	camelize : function(){
		return this.replace(/(-\S)/g,function($1){return $1.toUpperCase().substring(1,2)});
	},
	
	hasSubString : function(s,f){
		if(!f) f="";
		return (f+this+f).indexOf(f+s+f)==-1?false:true;
    },
    
	hasSubStrInArr : function(){
		for(var i=0; i<arguments[0].length; i++){
			if(this.hasSubString(arguments[0][i])){return true;}
		}
		return false;
	},
	
	toXML : function(){
		var arr = this.split("&");
		var str = new StringBuffer();
		for (var i=0,len=arr.length; i<len; i++){
			var item = arr[i].split("=");
			str.append("<"+item[0]+"><![CDATA["+item[1]+"]]></"+item[0]+">");
		}
		var rootStr = arguments[0]?arguments[0]:"root";
		return "<"+rootStr+">"+str.toString()+"</"+rootStr+">";
	},
	
	format : function(){
		var p = arguments;
		return this.replace(/(\{\d+\})/g,function(){
			return p[arguments[0].replace(/\D/g,"")];
		});		
	},
	
	uniq : function(){			
		var arr = this.split("");
		var obj = {};
		for(var i=0,j; j=arr[i]; i++){
			obj[j] = i;
		}
		var s = [];
		for(var key in obj){
			s[obj[key]]=key;
		}
		return s.join("");
	},
	
	trim : function (str){
		return str.replace(/^\s+|\s+$/ig,"");
	},

	trimL : function (str){
		return str.replace(/^\s+/ig,"");
	},

	trimR : function (str){
		return str.replace(/\s+$/ig,"");
	},

	LTrim : function (sString){
	    var sStr,i,iStart,sResult = "";
	    sStr = sString.split("");
	    iStart = -1 ;
	    for (i = 0 ; i < sStr.length ; i++){
	        if (sStr[i] != " "){
	            iStart = i;
	            break;
	        }
	    }
	    if (iStart == -1) { return "" ;}    //表示sString中的所有字符均是空格,则返回空串


	    else { return sString.substring(iStart) ;}
	},

	RTrim : function (sString){
	    var sStr,i,iEnd,sResult = "";
	    sStr = sString.split("");
	    iEnd = -1 ;
	    for (i = sStr.length-1 ; i >=0 ; i--){
	        if (sStr[i] != " "){
	        	iEnd = i;
	            break;
	        }
	    }
	    if (iEnd == -1) { return "" ;}    //表示sString中的所有字符均是空格,则返回空串


	    else { return sString.substring(iEnd) ;}
	},

	eval : function(attr){
		try{
			if (typeof(attr)!='string' || attr.length<1){
				return {};
			}else if(attr.substr(0,1)=='{'){
				return eval('('+attr+')');
			}else{
				return eval('({'+attr+'})');
			}
		}catch(e){
			alert("eval attr error:"+e);
			return {};
		}
	},

	none:""

};

$string = new $String();

$StringBuffer=$class.create("$lang.StringBuffer");
$StringBuffer.prototype = {
	initialize : function(){this.data = [];},
	append : function(){this.data.push(arguments[0]);return this},
	toString : function(){return this.data.join(arguments[0]||"")},
	length : function(){return this.data.length},
	clear : function(){this.data.length=0; return this;}
}
